﻿package mvc.view {
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import mvc.model.ToolProxy;
	import mvc.PainterFacade;
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	import mvc.view.*;
	
	public class PainterMediator extends Mediator implements IMediator {
	
		public static const NAME:String = "PainterMediator";
		
		private var _colorPicker	:Sprite;
		private var _pen			:Sprite;
		private var _eraser			:Sprite;
		private var _clear			:Sprite;
		private var _canvas			:Canvas;
		
		public function PainterMediator(viewComponent:Object) {
			super(NAME, viewComponent);
			
			_colorPicker = viewComponent[0];
			_pen = viewComponent[1];
			_eraser = viewComponent[2];
			_clear = viewComponent[3];
			_canvas = viewComponent[4];
			
			_colorPicker.addEventListener(Event.CHANGE, colorChanged);
			_pen.addEventListener(MouseEvent.CLICK, choosePen);
			_eraser.addEventListener(MouseEvent.CLICK, chooseEraser);
			_clear.addEventListener(MouseEvent.CLICK, clearCanvas);
			_canvas.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
			_canvas.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}
		
		private function mouseDown(e:MouseEvent):void {
			_canvas.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
			sendNotification(PainterFacade.MOUSE_DOWN, {x:_canvas.mouseX, y:_canvas.mouseY});
		}
		
		private function mouseUp(e:MouseEvent):void {
			_canvas.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
		}
		
		private function mouseMove(e:MouseEvent):void {
			sendNotification(PainterFacade.MOUSE_MOVE);
		}
		
		private function colorChanged(e:Event):void {
			sendNotification(PainterFacade.COLOR_CHANGE, colorPicker["selectedColor"]);
		}
		
		private function choosePen(e:MouseEvent):void {
			sendNotification(PainterFacade.TOOL_CHANGE, ToolProxy.PEN);
		}
		
		private function chooseEraser(e:MouseEvent):void {
			sendNotification(PainterFacade.TOOL_CHANGE, ToolProxy.ERASER);
		}
		
		private function clearCanvas(e:MouseEvent):void {
			sendNotification(PainterFacade.CLEAR);
		}
		
		override public function listNotificationInterests():Array {
			return [PainterFacade.CLEAR];
		}
		
		override public function handleNotification(notification:INotification):void {
			switch (notification.getName()) {
				case PainterFacade.CLEAR:
					canvas.bmp.bitmapData.fillRect(new Rectangle(0, 0, 640, 400), 0xFFFFFF);
					break;
			}
		}
		
		
		override public function getMediatorName():String {
			return PainterMediator.NAME;
		}
		
		public function get colorPicker():Sprite { return _colorPicker; }
		public function get pen():Sprite { return _pen; }
		public function get eraser():Sprite { return _eraser; }
		public function get clear():Sprite { return _clear; }
		public function get canvas():Canvas { return _canvas; }
	}
}
